<!DOCTYPE html>
<html lang="zh">
<head>
    <title>登录页面 - 派后台管理系统</title>
    {include file="public/header" /}
</head>
<body>
<div class="container-fluid p-t-15">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header"><h4>搜索</h4></div>
                <div class="card-body">
                    <form class="form-inline searchForm" onsubmit="return false;">
                        <div class="form-group">
                            <label for="add_time">留言时间</label>
                            <div class="input-group">
                                <input class="form-control js-datetimepicker" type="text" id="add_time" name="add_time" value="" data-side-by-side="true" data-locale="zh-cn" data-format="YYYY-MM-DD" placeholder="开始时间">
                                <span class="input-group-addon">~</span>
                                <input class="form-control js-datetimepicker" type="text" name="add_time" value="" data-side-by-side="true" data-locale="zh-cn" data-format="YYYY-MM-DD" placeholder="结束时间">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="type">来源</label>
                            <select name="type" id="type" class="form-control">
                                <option value="">所有</option>
                                <option value="1">CMS留言</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="is_read">是否已读</label>
                            <select name="is_read" class="form-control" id="is_read">
                                <option value="">全部</option>
                                <option value="0">未读</option>
                                <option value="1">已读</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-success" style="margin: -10px 0 0 10px;" id="search">搜索</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header"><h4>用户留言</h4></div>
                <div class="card-body">
                    <div id="toolbar" class="toolbar-btn-action">
                        <button id="btn_delete" type="button" class="btn btn-danger">
                            <span class="mdi mdi-window-close" aria-hidden="true"></span>删除
                        </button>
                    </div>
                    <table id="tb_departments"></table>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script type="text/javascript">
    $('#tb_departments').bootstrapTable({
        classes: 'table table-bordered table-hover table-striped',
        url: '/admin/user.user_message/lst',
        method: 'post',
        dataType : 'json',        // 因为本示例中是跨域的调用,所以涉及到ajax都采用jsonp,
        uniqueId: 'id',
        idField: 'id',             // 每行的唯一标识字段
        toolbar: '#toolbar',       // 工具按钮容器
        showColumns: true,         // 是否显示所有的列
        showRefresh: true,         // 是否显示刷新按钮
        responseHandler: function (res) {
            return {
                "total": res.count,
                "rows": res.data,
            };
        },
        pagination: true,                    // 是否显示分页
        sortOrder: "asc",                    // 排序方式
        queryParams: function(params) {
            var add_time = new Array();
            $("input[name='add_time']").each(function(){
                add_time.push($(this).val());
            });
            var temp = {
                is_read: $('#is_read option:selected').val(),
                type: $('#type option:selected').val(),
                add_time: add_time,
                limit: params.limit,         // 每页数据量
                page: (params.offset / params.limit) + 1,
            };
            return temp;
        },                                   // 传递参数
        sidePagination: "server",            // 分页方式：client客户端分页，server服务端分页
        pageNumber: 1,                       // 初始化加载第一页，默认第一页
        pageSize: 10,                        // 每页的记录行数
        pageList: [20, 50, 100],         // 可供选择的每页的行数
        columns: [{
            checkbox: true    // 是否显示复选框
        }, {
            field: 'id',
            title: '留言ID',
        }, {
            field: 'name',
            title: '用户昵称'
        }, {
            field: 'tel',
            title: '电话'
        }, {
            field: 'email',
            title: '邮箱'
        }, {
            field: 'type',
            title: '留言来源'
        }, {
            field: 'content',
            title: '内容',
        }, {
            field: 'ip',
            title: 'ip',
        }, {
            field: 'user_agent',
            title: 'UserAgent',
        }, {
            field: 'add_time',
            title: '创建时间',
        }, {
            field: 'is_read',
            title: '是否已读',
            formatter: function (value, row, index) {
                if (value == 0) {
                    is_checked = '';
                } else if (value == 1){
                    is_checked = 'checked="checked"';
                }
                var field = "is_read";
                result = '<label class="lyear-switch switch-primary lyear-status"><input type="checkbox" '+ is_checked +'><span  onClick="updateStatus('+ row.id +', '+ value +', \''+ field +'\')"></span></label>';
                return result;
            },
        }, {
            field: 'operate',
            title: '操作',
            formatter: btnGroup,  // 自定义方法
            events: {
                'click .edit-btn': function (event, value, row, index) {
                    iframe.createIframe('修改','/admin/user.user_message/edit?id='+row.id)
                },
                'click .del-btn': function (event, value, row, index) {
                    $.alert({
                        title: '系统提示',
                        content: '删除提醒',
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'btn-primary',
                                action: function(){
                                    $.post(url="/admin/cms.cms_tag/del",data={"id":row.id},function (res) {
                                        if (res.status == 200) {parent.lightyear.notify('删除成功', 'success', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');$("#tb_departments").bootstrapTable('refresh');}
                                        else parent.lightyear.notify('删除失败', 'danger', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
                                    });
                                }
                            },
                            cancel: {
                                text: '取消'
                            }
                        }
                    });
                }
            }
        }],
        onLoadSuccess: function(data){
            $("[data-toggle='tooltip']").tooltip();
        }
    });

    // 操作按钮
    function btnGroup ()
    {
        let html =
            '<a href="#!" class="btn btn-xs btn-default del-btn" title="删除" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>';
        return html;
    }

    $("#search").click(function () {
        $('#tb_departments').bootstrapTable('refresh', {
            url: '/admin/user.user_message/lst',
        });
    });
    function updateStatus(id, value, field) {
        var newstate = (value == 1) ? 0 : 1; // 发送参数值跟当前参数值相反
        $.ajax({
            type: "post",
            url: "/admin/user.user_message/field?id="+id,
            data: {field: field, value: newstate},
            dataType: 'json',
            success: function (res) {
                if (res.status == 200) {parent.lightyear.notify('修改成功', 'success', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');$(".tree-table").bootstrapTable('refresh');}
                else parent.lightyear.notify('修改失败', 'danger', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
            },
            error: function () {
                parent.lightyear.notify('修改失败', 'danger', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
            }
        });
    }
</script>
</body>
</html>